﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Base;
using ERP.BusinessEntities;
using System.Data;
using Core;
using Utilities;

namespace MainApp.Modules.Transfer
{
    public class TransferEntity: BaseEntity
    {
        SGVList<ICTransferItemsInfo> lstICTransferItems = new SGVList<ICTransferItemsInfo>();

        public override void InitModuleObjects()
        {
            AddMainObject("ICTransfers", new ICTransfersInfo());
        }

        public override void InitLists()
        {
            base.InitLists();

            lstICTransferItems = new SGVList<ICTransferItemsInfo>();
            lstICTransferItems.InitList(this, "ICTransfers", "ICTransferItems", "FK_ICTransfers_ID");

            lstICTransferItems.Binding("MainScreen", "ICTransferItemsGridView");
        }

        public override void BeforeSaveList()
        {
            base.BeforeSaveList();

            ICTransfersInfo Transfer = (ICTransfersInfo)MainObject;
            if (Transfer == null) return;

            DataSet dsTransferItems = AppContext.TransferItemCtrl.GetAllObjectByColumnValue("FK_ICTransfers_ID", Transfer.ID);
            if (dsTransferItems != null && dsTransferItems.Tables.Count > 0)
            {
                foreach (DataRow drTransferItem in dsTransferItems.Tables[0].Rows)
                {
                    ICTransferItemsInfo TransferItem = (ICTransferItemsInfo)AppContext.TransferItemCtrl.GetObjectFromDataRow(drTransferItem);
                    if (TransferItem != null)
                    {
                        Inventory.UpdateInventory(InventoryDirection.In, InventoryType.OnHand, TransferItem.FK_ICProducts_ID, TransferItem.FK_ICStocks_FromID,
                            TransferItem.BatchNo, TransferItem.LotNo, TransferItem.SerialNo, TransferItem.Qty);
                        Inventory.UpdateInventory(InventoryDirection.Out, InventoryType.OnHand, TransferItem.FK_ICProducts_ID, TransferItem.FK_ICStocks_ToID,
                            TransferItem.BatchNo, TransferItem.LotNo, TransferItem.SerialNo, TransferItem.Qty);
                    }
                }
            }

        }

        public override void AfterSaveList()
        {
            base.AfterSaveList();

            ICTransfersInfo Transfer = (ICTransfersInfo)MainObject;
            if (Transfer == null) return;

            DataSet dsTransferItems = AppContext.TransferItemCtrl.GetAllObjectByColumnValue("FK_ICTransfers_ID", Transfer.ID);
            if (dsTransferItems != null && dsTransferItems.Tables.Count > 0)
            {
                foreach (DataRow drTransferItem in dsTransferItems.Tables[0].Rows)
                {
                    ICTransferItemsInfo TransferItem = (ICTransferItemsInfo)AppContext.TransferItemCtrl.GetObjectFromDataRow(drTransferItem);
                    if (TransferItem != null)
                    {
                        Inventory.UpdateInventory(InventoryDirection.Out, InventoryType.OnHand, TransferItem.FK_ICProducts_ID, TransferItem.FK_ICStocks_FromID,
                            TransferItem.BatchNo, TransferItem.LotNo, TransferItem.SerialNo, TransferItem.Qty);

                        Inventory.UpdateInventory(InventoryDirection.In, InventoryType.OnHand, TransferItem.FK_ICProducts_ID, TransferItem.FK_ICStocks_ToID,
                            TransferItem.BatchNo, TransferItem.LotNo, TransferItem.SerialNo, TransferItem.Qty);
                    }

                }
            }

        }
    }
}
